﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using NUnit.Framework;
using Microsoft.SharePoint;
using SharePoint.Contrib;
using SharePoint.Contrib.Extensions;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class LibraryBuilderTest
    {
        SPSite sitecollection = null;
        SPWeb root = null;
        List<Guid> created;

        [SetUp]
        public void Init()
        {
            created = new List<Guid>();

            if (SharePoint.TryFindSiteCollection(out sitecollection))
            {
                root = sitecollection.RootWeb;
            }
        }

        [TearDown]
        public void Dispose()
        {
            created.ForEach(id =>
                {
                    root.Lists[id].Delete();
                });
            
            sitecollection.Dispose();
        }


        [Test]
        public void PropertiesCanBeSetThroughFluentInterface()
        {
           var builder = new LibraryBuilder()
                .Name("mylib")
                .ContentTypeSupportFor(SPBuiltInContentTypeId.Task)
                .ContentTypeSupportFor(SPBuiltInContentTypeId.XMLDocument)
                .Description("This is an instance of mylib")
                .CanBeCrawled(false)
                .SupportsVersioning(true)
                .Template(SPListTemplateType.DocumentLibrary)
                .Title("My Lib");

 
            Assert.That(builder.Name, Is.EqualTo("mylib"));
            Assert.That(builder.Description, Is.EqualTo("This is an instance of mylib"));
            Assert.True(builder.SupportedContentTypes.Exists(id => id == SPBuiltInContentTypeId.Task));
            Assert.True(builder.SupportedContentTypes.Exists(id => id == SPBuiltInContentTypeId.XMLDocument));
            Assert.That(builder.CanBeCrawled, Is.False);
            Assert.That(builder.SupportsVersioning, Is.True);
            Assert.That(builder.TemplateType, Is.EqualTo(SPListTemplateType.DocumentLibrary));
            Assert.That(builder.Title, Is.EqualTo("My Lib"));
            
        }

        [Test]
        public void CanProvideLoggingInformation()
        {
            var builder = new LibraryBuilder(root, "mytasklist2")
                 .ContentTypeSupportFor(SPBuiltInContentTypeId.Task)
                 .Description("This is an instance of mylib")
                 .CanBeCrawled(false)
                 .SupportsVersioning(true)
                 .Title("My Task List 2");

            string log = string.Empty;
            
            builder.Logging += (sender, args) =>
                {
                    log = args.Value;
                };

            var list = builder.Cast<SPList>();

            Assert.That(log, Is.Not.Empty);
            created.Add(list.ID);
        }

        [Test]
        public void CanCreateListInstance()
        {
            var builder = new LibraryBuilder(root, "mytasklist")
                 .ContentTypeSupportFor(SPBuiltInContentTypeId.Task)
                 .Description("This is an instance of mylib")
                 .CanBeCrawled(false)
                 .SupportsVersioning(true)
                 .Title("My Task List");

            var list = builder.Cast<SPList>();

            Assert.NotNull(list);
            Assert.That(list.Title, Is.EqualTo("My Task List"));

            created.Add(list.ID);
        }

        [Test]
        public void CanCreateDocLibInstance()
        {
            var builder = new LibraryBuilder
            {
                Site = root,
                Name = "mydoclib",
                Title = "My Doc Lib",
                Description = "...",
                TemplateType = SPListTemplateType.DocumentLibrary
            };

            builder.SupportedContentTypes.Add(SPBuiltInContentTypeId.XMLDocument);
 
            var doclib = builder.Cast<SPDocumentLibrary>();

            Assert.NotNull(doclib);
            Assert.That(doclib.Title, Is.EqualTo("My Doc Lib"));
            Assert.That(doclib.ContainsContentType(SPBuiltInContentTypeId.XMLDocument), Is.True);

            created.Add(doclib.ID);
        }



    }
}